package ab.mes.controller;

import ab.mes.entity.Material;
import ab.mes.service.MaterialService;
import ab.mes.tools.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/material")
public class MaterialController {
    @Autowired
    private MaterialService materialService;

    //获取物料信息
    @PostMapping("/getMaterial")
    public Result getMaterial(){
        Result result=new Result();
        try {
            result = materialService.getMaterial();
        }catch (Exception e){
            result.setErrorMessage("查询物料失败！");
            e.printStackTrace();
        }
        return result;
    }

    //通过物料编号获取物料的名字、型号和描述
    @PostMapping("/getMaterialByNumber")
    public Result getMaterialByNumber(Integer id_Of_Material){
        Result result=new Result();
        try {
            result = materialService.getMaterialByNumber(id_Of_Material);
        }catch (Exception e){
            result.setErrorMessage("查询物料失败！");
            e.printStackTrace();
        }
        return result;
    }
    //新增物料信息
    @PostMapping("/insertMaterial")
    public Result insertMaterial(@RequestBody Material material){
        Result result=new Result();
        try {
            result = materialService.insertMaterial(material);
        }catch (Exception e){
            result.setErrorMessage("新增物料失败！");
            e.printStackTrace();
        }
        return result;
    }
    //根据id删除物料信息
    @PostMapping("/deleteMaterial")
    public Result deleteMaterial(Integer id_Of_Material){
        Result result = new Result();
        try{
            result = materialService.deleteMaterial(id_Of_Material);
        }catch (Exception e){
            result.setErrorMessage("删除物料失败！");
            e.printStackTrace();
        }
        return result;
    }
    //修改物料信息
    @PostMapping("/editMaterial")
    public Result editMaterial(@RequestBody Material material){
        Result result = new Result();
        try{
            result = materialService.editMaterial(material);
        }catch (Exception e){
            result.setErrorMessage("修改物料失败！");
            e.printStackTrace();
        }
        return result;
    }
}
